package com.sumer.psychology.common.persistence.mongodb;

import com.mongodb.BasicDBList;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

/**
 * 带有标记的mongo资源库
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/10/30
 * Time: 8:58
 */
public abstract class AbstractTaggedMongoRepository extends AbstractMongoRepository {
    /**
     * 查找一个业务下的所有tags
     *
     * @param limit limit
     * @return json
     */
    public String findTags(int limit) {
        DBCursor results = db().getCollection(dbName())
                .find()
                .limit(limit);

        for (DBObject result : results.toArray()) {
            result.put("size", ((BasicDBList) result.get(type() + "_ids")).size());
        }
        return serialize(results);
    }

    protected abstract String type();
}
